package com.example.demo.config;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: sfx
 * Date: 2023-01-16
 * Time: 21:06
 */

/**
 * 通用化的工具类
 */
//登陆拦截器->自定义拦截器
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        HttpSession session = request.getSession(false);
        if(session!=null&&session.getAttribute("userinfo")!=null) {
            //登陆成功
            log.info("登陆成功");
            return true;
        }
        //没有访问权限
        log.error("没有访问权限");
        response.setStatus(401);
        return false;
    }
}